An extension of the Poisson model for count data that exhibits overdispersion.
General Principles
To model the relationship between a count outcome variable and one or more independent variables with overdispersion 🛈, we can use the Negative Binomial model.
Considerations
Caution
We have the same considerations as for the Poisson model.
Overdispersion is handled because the Gamma-Poisson model assumes that each Poisson count observation has its own rate. This is an additional parameter specified in the model (in the code, it is log_days).
Example
Below is an example code snippet demonstrating a Bayesian Gamma-Poisson model using the BayesForge (BF) package.
from BayesForge import bfimport jax.numpy as jnp# Setup device ------------------------------------------------m = bf(platform='cpu') # Import# Import Data & Data Manipulation ------------------------------------------------# Importfrom importlib.resources import filesdata_path = m.load.sim_gamma_poisson(only_path=True)m.data(data_path, sep=',') # Define model ------------------------------------------------def model(log_days, monastery, y): a = m.dist.normal(0, 1, name ='a', shape=(1,)) b = m.dist.normal(0, 1, name ='b', shape=(1,)) phi = m.dist.exponential(1, name ='phi', shape=(1,)) mu = jnp.exp(log_days + a + b * monastery) Lambda = m.dist.gamma(concentration = mu*phi, rate = phi, name ='Lambda') m.dist.poisson(rate = Lambda, obs=y)# Run MCMC ------------------------------------------------m.fit(model, progress_bar=False) # Optimize model parameters through MCMC sampling# Summary ------------------------------------------------m.summary() # Get posterior distributions
An NVIDIA GPU may be present on this machine, but a CUDA-enabled jaxlib is not installed. Falling back to cpu.
bf v 0.0.60 package loaded
jax.local_device_count(cpu) = 32
mean
sd
eti89_lb
eti89_ub
ess_bulk
ess_tail
r_hat
mcse_mean
mcse_sd
Lambda[0]
1.47
0.33
0.99
2.05
6376.33
2858.03
1.00
0.00
0.01
Lambda[1]
1.47
0.33
0.98
2.03
6420.51
2767.91
1.00
0.00
0.01
Lambda[2]
1.61
0.34
1.10
2.19
5636.52
3013.74
1.00
0.00
0.01
Lambda[3]
1.55
0.35
1.03
2.14
5777.35
2791.34
1.00
0.00
0.01
Lambda[4]
1.46
0.33
0.99
2.01
5638.98
2593.89
1.00
0.00
0.01
...
...
...
...
...
...
...
...
...
...
Lambda[3398]
3.27
0.51
2.49
4.14
4519.28
2120.45
1.00
0.01
0.01
Lambda[3399]
3.19
0.50
2.44
3.99
5117.26
2516.12
1.00
0.01
0.01
a[0]
0.41
0.02
0.38
0.43
428.44
544.65
1.01
0.00
0.00
b[0]
-1.14
0.03
-1.19
-1.09
428.61
752.12
1.01
0.00
0.00
phi[0]
12.45
2.26
9.27
16.35
58.39
104.03
1.02
0.30
0.14
3403 rows × 9 columns
library(BayesForge)# Setup platform------------------------------------------------m=importBF(platform='cpu')# Import data ------------------------------------------------m$data(m$load$sim_gamma_poisson(only_path=T), sep =',')m$data_to_model(list('log_days', 'monastery', 'y' )) # Send to model (convert to jax array)# Define model ------------------------------------------------model <-function(log_days, monastery, y){# Parameter prior distributions a = m$dist$normal(0, 1, name='a', shape=reticulate::tuple(1L)) b = m$dist$normal(0, 1, name='b', shape=reticulate::tuple(1L)) phi = m$dist$exponential(1, name='phi', shape=reticulate::tuple(1L)) mu = jnp$exp(log_days + a + b * monastery) Lambda = m$dist$gamma(concentration = mu*phi, rate = phi, name ='Lambda')# Likelihood m$dist$poisson(rate=Lambda, obs=y, name ="y")}# Run MCMC ------------------------------------------------m$fit(model, progress_bar=FALSE) # Optimize model parameters through MCMC sampling# Summary ------------------------------------------------m$summary() # Get posterior distributions
usingPyBayesForge# Setup device------------------------------------------------m =importBF(platform="cpu")# Import Data & Data Manipulation ------------------------------------------------# Importdata_path = m.load.sim_gamma_poisson(only_path =true)m.data(data_path, sep=',')# Define model ------------------------------------------------@BFfunctionmodel(log_days, monastery, y) a = m.dist.normal(0, 1, name ="a", shape=(1,)) b = m.dist.normal(0, 1, name ="b", shape=(1,)) phi = m.dist.exponential(1, name ="phi", shape=(1,)) mu = jnp.exp(log_days + a + b * monastery) Lambda = m.dist.gamma(concentration = mu*phi, rate = phi, name ="Lambda") m.dist.poisson(rate = Lambda, obs=y)end# Run mcmc ------------------------------------------------m.fit(model) # Optimize model parameters through MCMC sampling# Summary ------------------------------------------------m.summary() # Get posterior distributions
Mathematical Details
Bayesian model
In the Bayesian formulation, we define each parameter with priors 🛈. We can express the Bayesian regression model accounting for prior distributions as follows:
Y_i \sim \text{Poisson}(\lambda_i)
\lambda_i \sim \text{Gamma}(\mu_i \phi, \phi)
\log(\mu_i) = \text{rates}_i + \alpha + \beta X_i
\alpha \sim \text{Normal}(0,1)
\beta \sim \text{Normal}(0,1)
\phi \sim \text{Exponential}(1)
Where:
Y_i is the dependent variable for observation i.
\lambda_i is the rate parameter of the Poisson distribution for observation i, assuming that each Poisson count observation has its own rate_i.
\mu_i is the mean rate parameter.
\phi controls the level of overdispersion in the rates.
\alpha is the intercept term.
\beta is the regression coefficient.
X_i is the value of the predictor variable for observation i.
Model validation tests
Comparison with Stan: 7.Negative binomial.py fits the same model (negative binomial with an exposure offset) with BayesForge and Stan and compares the posterior distributions (KL divergence).
Data generative model and inference model pipeline: 7.Negative binomial.py simulates data from the model with known parameters (data generative model), fits the inference model to each simulated dataset and checks that the parameters are recovered.